package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;



    @RequiredPermission(name = "权限列表", expression = "permission:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageResult<Permission> pageResult = permissionService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "permission/list";
    }

    @RequiredPermission(name = "权限删除", expression = "permission:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            if (id != null) {
                permissionService.delete(id);
            }
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "删除失败");
        }
    }

    @RequiredPermission(name = "权限加载", expression = "permission:reload")
    @RequestMapping("/reload")
    @ResponseBody
    public JSONResult reload() {
        try {
            permissionService.reload();
            return new JSONResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false, "加载失败");
        }
    }
}
